package com.joesem.sale.client.logic.notice

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.FragmentOrderContentBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.logic.workOrder.WorkOrderDetailsActivity
import com.joesem.sale.client.network.data.NoticeListMode
import com.joesem.sale.client.utils.click

class NoticeOrderFragment : BaseFragment<FragmentOrderContentBinding>() {

    private val viewMode by createViewModel<NoticeViewModel>()

    private val adapter =
        object : BaseQuickAdapter<NoticeListMode, BaseViewHolder>(R.layout.item_notice_order) {
            var itemClick: (NoticeListMode) -> Unit = {}
            override fun convert(holder: BaseViewHolder, item: NoticeListMode) {
                holder.setText(R.id.tvNoticeStats, item.title.orEmpty())
                holder.setText(R.id.tvNoticeDate, item.update_time_format.orEmpty())
                holder.setText(R.id.tvUserName, "负责人：" + item.user_name.orEmpty())
                holder.setText(R.id.tvCustomer, "客户: " + item.customer_name.orEmpty())
                holder.setText(R.id.tvContactName, "联系人: " + item.contact_name.orEmpty())
                holder.setText(R.id.tvContactPhone, "电话: " + item.contact_phone.orEmpty())
                holder.setText(R.id.tvAddress, "地址: " + item.address.orEmpty())
                holder.setText(R.id.tvPlanDate, "计划时间: " + item.plan_at.orEmpty())
                holder.getView<TextView>(R.id.tvNoticeStats).apply {
                    setTextColor(if (item.status == 1) R.color.color666.color() else R.color.black.color())
                }
                holder.getView<ImageView>(R.id.ivNoticeIcon).apply {
                    setImageResource(if (item.status == 1) R.drawable.ic_email else R.drawable.ic_email_default)
                }
                holder.getView<View>(R.id.cvNoticePoint).apply {
                    if (item.status == 1) gone() else visible()
                }
                holder.itemView.click {
                    itemClick.invoke(item)
                    item.status = 1
                    notifyItemChanged(holder.adapterPosition)
                }
            }
        }

    override fun FragmentOrderContentBinding.initView(savedInstanceState: Bundle?) {
        rvContent.adapter = adapter
        srlRefresh.autoRefresh()
    }

    override fun FragmentOrderContentBinding.initListener() {
        lifecycle.addObserver(object :
            DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                srlRefresh.autoRefresh()
            }
        })
        adapter.itemClick = {
            launch<WorkOrderDetailsActivity>("orderId" to it.business_id.toString())
            viewMode.noticeRead(it.id)
        }
        srlRefresh.apply {
            setOnRefreshListener {
                viewMode.getNoticeList(true, NOTICE_TYPE_ORDER)
            }
            setOnLoadMoreListener {
                viewMode.getNoticeList(false, NOTICE_TYPE_ORDER)
            }
        }
    }

    override fun FragmentOrderContentBinding.initIntent(intent: Any?) {
        when (intent) {
            is NoticeViewModel.NoticeListIntent -> {
                srlRefresh.apply {
                    finishRefresh()
                    finishLoadMore()
                }
                adapter.apply {
                    if (intent.isFirst)
                        setList(intent.data)
                    else
                        addData(intent.data)
                }
            }
        }
    }
}